package com.example.diandian.service.edu.controller.admin;


import com.example.diandian.common.base.result.R;
import com.example.diandian.service.edu.entity.Chapter;
import com.example.diandian.service.edu.entity.vo.ChapterVo;
import com.example.diandian.service.edu.service.ChapterService;
import com.example.diandian.service.edu.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author JTJ
 * @since 2020-07-19
 */
@RestController
@RequestMapping("/admin/edu/chapter")
//@CrossOrigin
@Api("章节管理")
@Slf4j
public class ChapterController {
    @Autowired
    private ChapterService chapterService;

    @Autowired
    private VideoService videoService;

    @ApiOperation("新增章节")
    @PostMapping("save")
    public R save(
            @ApiParam(value = "章节对象",required = true)
            @RequestBody Chapter chapter){
        boolean save = chapterService.save(chapter);
        if (save){
            return R.ok().message("保存成功");
        }else {
            return R.error().message("保存失败");
        }
    }

    @ApiOperation("根据id查询章节")
    @GetMapping("get/{id}")
    public R getById(
            @ApiParam(value = "章节ID",required = true)
            @PathVariable String id){
        Chapter byId = chapterService.getById(id);
        if (byId != null){
            return R.ok().data("item",byId);
        }else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("更新章节")
    @PutMapping("update")
    public R updateById(
            @ApiParam(value = "章节对象",required = true)
            @RequestBody Chapter chapter){
        boolean b = chapterService.updateById(chapter);
        if (b){
            return R.ok().message("修改成功");
        }else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("删除章节")
    @DeleteMapping("remove/{id}")
    public R deleteById(
            @ApiParam(value = "章节ID",required = true)
            @PathVariable String id){
        //删除视频：VOD
        videoService.removeMediaVideoByChapterId(id);

        boolean b = chapterService.removeChapterById(id);
        if (b){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("嵌套章节数据列表")
    @GetMapping("nested-list/{courseId}")
    public R nestedListByCourseId(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String courseId){

        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
        return R.ok().data("items", chapterVoList);
    }

}

